#Developed by Daniele Perrone
#Research Assistent
#MCEER Earthquake Engineering to Extreme Event
#University at Buffalo - State University of New York

 
proc DynamicAnalysisSolver {dt DurationOfSim Tolerance} {


#--------------------------------------------------------------------------------------------------
# Analysis Parameters
#--------------------------------------------------------------------------------------------------

wipeAnalysis
constraints Transformation
numberer RCM
system UmfPack -lvalueFact 30   #original option: system BandGeneral
# system BandGeneral
# test RelativeEnergyIncr $Tolerance 80 
set testid NormDispIncr
test $testid $Tolerance 800 
algorithm Newton
integrator Newmark 0.50 0.25
analysis Transient


#--------------------------------------------------------------------------------------------------
# Run Analysis
#--------------------------------------------------------------------------------------------------

set tCurrent [getTime]; 
set CollapseFlag "OFF"; 
set ok 0;

while {$ok == 0 && $tCurrent < $DurationOfSim} {

#--------------------------------------------------------------------------------------------------
# Start with Newton-Raphson with "dt"
#--------------------------------------------------------------------------------------------------

test $testid $Tolerance 400 
algorithm Newton
set ok [analyze 1 $dt]


#--------------------------------------------------------------------------------------------------
# If no convergence, try Newton with Line Search with "dt"
#--------------------------------------------------------------------------------------------------

if {$ok != 0} {
puts "+----------------------------------------------------------------------+";
puts "| Try Newton-Raphson with Line Search with -dt-                        |";
puts "+----------------------------------------------------------------------+";
test $testid $Tolerance 800 
algorithm NewtonLineSearch
set ok [analyze 1 $dt]
}


#--------------------------------------------------------------------------------------------------
# If no convergence, try Modified Newton-Raphson with "dt"
#--------------------------------------------------------------------------------------------------

if {$ok != 0} {
puts "+----------------------------------------------------------------------+";
puts "| Try Modified Newton-Raphson with -dt-                                |";
puts "+----------------------------------------------------------------------+";
test $testid $Tolerance 2000 
algorithm ModifiedNewton -initial
set ok [analyze 1 $dt]
}



#--------------------------------------------------------------------------------------------------
# If no convergence, try Newton with Line Search with "0.5*dt"
#--------------------------------------------------------------------------------------------------

if {$ok != 0} {
puts "+----------------------------------------------------------------------+";
puts "| Try Newton-Raphson with -1/2*dt-                                     |";
puts "+----------------------------------------------------------------------+";
test $testid $Tolerance 800 
algorithm Newton
set ok [analyze 1 [expr 0.5*$dt]]
}


#--------------------------------------------------------------------------------------------------
# If no convergence, try Newton with Line Search with "0.5*dt"
#--------------------------------------------------------------------------------------------------

if {$ok != 0} {
puts "+----------------------------------------------------------------------+";
puts "| Try Newton-Raphson with Line Search with -1/2*dt-                    |";
puts "+----------------------------------------------------------------------+";
test $testid $Tolerance 800 
algorithm NewtonLineSearch
set ok [analyze 1 [expr 0.5*$dt]]
}


#--------------------------------------------------------------------------------------------------
# If no convergence, try Modified Newton-Raphson with "0.5*dt"
#--------------------------------------------------------------------------------------------------

if {$ok != 0} {
puts "+----------------------------------------------------------------------+";
puts "| Try Modified Newton-Raphson with -1/2*dt-                            |";
puts "+----------------------------------------------------------------------+";
test $testid $Tolerance 2000 
algorithm ModifiedNewton -initial
set ok [analyze 1 [expr 0.5*$dt]]
}


#--------------------------------------------------------------------------------------------------
# If no convergence, try Newton with Line Search with "0.1*dt"
#--------------------------------------------------------------------------------------------------

if {$ok != 0} {
puts "+----------------------------------------------------------------------+";
puts "| Try Newton-Raphson with -1/10*dt-                                    |";
puts "+----------------------------------------------------------------------+";
test $testid $Tolerance 800 
algorithm Newton
set ok [analyze 1 [expr 0.1*$dt]]
}


#--------------------------------------------------------------------------------------------------
# If no convergence, try Newton with Line Search with "0.1*dt"
#--------------------------------------------------------------------------------------------------

if {$ok != 0} {
puts "+----------------------------------------------------------------------+";
puts "| Try Newton-Raphson with Line Search with -1/10*dt-                   |";
puts "+----------------------------------------------------------------------+";
test $testid $Tolerance 800 
algorithm NewtonLineSearch
set ok [analyze 1 [expr 0.1*$dt]]
}


#--------------------------------------------------------------------------------------------------
# If no convergence, try Modified Newton-Raphson with "0.1*dt"
#--------------------------------------------------------------------------------------------------

if {$ok != 0} {
puts "+----------------------------------------------------------------------+";
puts "| Try Modified Newton-Raphson with -1/10*dt-                           |";
puts "+----------------------------------------------------------------------+";
test $testid $Tolerance 2000 
algorithm ModifiedNewton -initial
set ok [analyze 1 [expr 0.1*$dt]]
}



#--------------------------------------------------------------------------------------------------
# Get Current Time
#--------------------------------------------------------------------------------------------------
set tCurrent [getTime]
}


#--------------------------------------------------------------------------------------------------
# Final Message
#--------------------------------------------------------------------------------------------------

if {$ok == 0} {
puts "  Analysis has successfully concluded, Time = [getTime]"
} else {
puts "  Analysis could not be completed, Time = [getTime]"
}

}